function toggleNames(module) {
    var namesList = document.getElementById('names-' + module);
    var showMoreBtn = document.getElementById('show-more-' + module);
    var showLessBtn = document.getElementById('show-less-' + module);
    var hiddenItems = namesList.querySelectorAll('.name-item.hidden');

    if (showLessBtn.style.display === 'inline-block') {
        // 收起操作
        hiddenItems.forEach(function (item) {
            item.style.display = 'none';
        });
        showMoreBtn.style.display = 'inline-block';
        showLessBtn.style.display = 'none';
        namesList.classList.remove('show-all');
    } else {
        // 展开操作
        hiddenItems.forEach(function (item) {
            item.style.display = 'block';
        });
        showMoreBtn.style.display = 'none';
        showLessBtn.style.display = 'inline-block';
        namesList.classList.add('show-all');
    }
}

function loadMore(module) {
    var namesList = document.getElementById('names-' + module);
    var hiddenItems = namesList.querySelectorAll('.name-item.hidden');
    hiddenItems.forEach(function (item) {
        item.style.display = 'block';
    });
    // 隐藏“查看更多”按钮
    document.getElementById('show-more-' + module).style.display = 'none';
    // 显示“收起”按钮
    document.getElementById('show-less-' + module).style.display = 'inline-block';
    namesList.classList.add('show-all');
}

// 新增函数，根据高度展示数据
function showDataBasedOnHeight() {
    var nameLists = document.querySelectorAll('.name-list');
    nameLists.forEach(function (list) {
        var module = list.dataset.module;
        var allItems = list.querySelectorAll('.name-item');
        var visibleHeight = list.offsetHeight;
        var currentHeight = 0;
        var showMoreBtn = document.getElementById('show-more-' + module);
        var showLessBtn = document.getElementById('show-less-' + module);

        allItems.forEach(function (item, index) {
            if (currentHeight + item.offsetHeight <= visibleHeight) {
                item.style.display = 'block';
                currentHeight += item.offsetHeight;
            } else {
                item.style.display = 'none';
                item.classList.add('hidden');
                if (index < allItems.length - 1) {
                    showMoreBtn.style.display = 'inline-block';
                    showLessBtn.style.display = 'none';
                    list.classList.remove('show-all');
                }
            }
        });
    });
}

// 封装一个函数来获取图片并缓存
async function getImageWithCache(url) {
    const cacheKey = `image_${url}`;
    const cachedImage = localStorage.getItem(cacheKey);

    // 检查缓存是否存在且未过期
    if (cachedImage) {
        const { data, expiration } = JSON.parse(cachedImage);
        if (expiration > Date.now()) {
            return data;
        }
    }

    try {
        // 若缓存不存在或已过期，从网络获取图片
        const response = await fetch(`/image/${encodeURIComponent(url)}`);
        if (response.ok) {
            const blob = await response.blob();
            const reader = new FileReader();
            reader.readAsDataURL(blob);
            await new Promise((resolve) => {
                reader.onloadend = resolve;
            });
            const dataUrl = reader.result;

            // 设置缓存期限，例如 1 小时（可按需调整）
            const expiration = Date.now() + 1 * 60 * 60 * 1000;
            localStorage.setItem(cacheKey, JSON.stringify({ data: dataUrl, expiration }));

            return dataUrl;
        }
    } catch (error) {
        console.error('Failed to load image:', error);
    }
    return null;
}

// 修改显示图片的逻辑，使用缓存函数
async function displayImages() {
    const imageContainers = document.querySelectorAll('.image-container img');
    for (const img of imageContainers) {
        const url = img.src.split('/image/')[1];
        const dataUrl = await getImageWithCache(url);
        if (dataUrl) {
            img.src = dataUrl;
        }
    }
}

// 页面加载完成后调用显示图片的函数
window.onload = function () {
    displayImages();
};

// 页面加载完成后，调用 showDataBasedOnHeight 函数
window.onload = function () {
    showDataBasedOnHeight();
};